import os
from osgeo import ogr,osr,gdal
def CreateShapeGeometry():
    # Create a Point
    point = ogr.Geometry(ogr.wkbPoint)
    point.AddPoint(114.32716369628906,30.573715834861623)
    print(point.ExportToWkt())

    # Create a LineString
    line = ogr.Geometry(ogr.wkbLineString)
    line.AddPoint(114.32291507720947,30.578888565037637)
    line.AddPoint(114.32716369628906,30.573715834861623)
    line.AddPoint(114.334716796875,30.57737372262836)
    line.AddPoint(114.32991027832031,30.585058532281685)
    print(line.ExportToWkt())

    # Create a Polygon
    # Create ring
    ring = ogr.Geometry(ogr.wkbLinearRing)
    ring.AddPoint(114.32291507720947,30.578888565037637)
    ring.AddPoint(114.32716369628906,30.573715834861623)
    ring.AddPoint(114.334716796875,30.57737372262836)
    ring.AddPoint(114.32991027832031,30.585058532281685)
    ring.AddPoint(114.32291507720947,30.578888565037637)
    # Create polygon
    poly = ogr.Geometry(ogr.wkbPolygon)
    poly.AddGeometry(ring)
    print(poly.ExportToWkt())

    # Create test polygon
    ring = ogr.Geometry(ogr.wkbLinearRing)
    ring.AddPoint(114.32291507720947,30.578888565037637)
    ring.AddPoint(114.32716369628906,30.573715834861623)
    ring.AddPoint(114.334716796875,30.57737372262836)
    ring.AddPoint(114.32991027832031,30.585058532281685)
    ring.AddPoint(114.32291507720947,30.578888565037637)
    poly = ogr.Geometry(ogr.wkbPolygon)
    poly.AddGeometry(ring)

def createShpfile():
    # Save a new Shapefile
    outShapefile = r"data/hubu.shp" #文件名
    outDriver = ogr.GetDriverByName("ESRI Shapefile") #数据驱动类型
    # 判断是否已有该数据，如果有则删除
    if os.path.exists(outShapefile):
        outDriver.DeleteDataSource(outShapefile)
    ds =outDriver.CreateDataSource(outShapefile)
    targetRS = osr.SpatialReference() #定义空间参考对象
    # http://epsg.io/  #https://epsg.org/search/by-name
    #用EPSG代号定义空间参考信息，4326为WGS1984地理坐标系
    targetRS.ImportFromEPSG(4326)
    outLayer=ds.CreateLayer('hubu',targetRS,geom_type=ogr.wkbPolygon)

    # Create test polygon
    ring = ogr.Geometry(ogr.wkbLinearRing) #线
    ring.AddPoint(114.32285070419314,
                   30.57892551211783)
    ring.AddPoint(114.32443857192993,
                  30.577558460774558)
    ring.AddPoint(114.32469606399536,
                  30.577299827271318)
    ring.AddPoint(114.32495355606078,
                  30.576727136345028)
    ring.AddPoint(114.32568311691284,
                  30.575138363165852)
    ring.AddPoint(114.32709932327275,
                  30.573752783912695)
    ring.AddPoint(114.32754993438719,
                  30.57360498762394)
    ring.AddPoint(114.32838678359985,
                  30.573919054468277)
    ring.AddPoint(114.32913780212402,
                  30.57386363098143)
    ring.AddPoint(114.32945966720581,
                  30.5734387165299)
    ring.AddPoint(114.33134794235232,
                  30.574214645863282)
    ring.AddPoint(114.32958841323855,
                  30.57813114679117)
    ring.AddPoint(114.33212041854858,
                  30.580089337937473)
    ring.AddPoint(114.33158397674563,
                   30.581271622878855)
    ring.AddPoint(114.33246374130249,
                   30.58175192201823)
    ring.AddPoint(114.33175563812257,
                  30.583396004905637)
    ring.AddPoint(114.32984590530397,
                  30.585095477010636)
    ring.AddPoint(114.32285070419314,
                   30.57892551211783)
    poly = ogr.Geometry(ogr.wkbPolygon)#多边形
    poly.AddGeometry(ring) #将线连成多边形

    fieldName1 = 'name'
    fieldType1 = ogr.OFTString
    fieldValue1 = 'hubu'
    fieldName2 = 'code'
    fieldType2 = ogr.OFTString
    fieldValue2 = 'B001B06EAF'
    # create a field
    nameField = ogr.FieldDefn(fieldName1, fieldType1)
    codeField = ogr.FieldDefn(fieldName2, fieldType2)
    outLayer.CreateField(nameField)
    outLayer.CreateField(codeField)

    # Create the feature and set values
    featureDefn = outLayer.GetLayerDefn()
    outFeature = ogr.Feature(featureDefn)
    outFeature.SetGeometry(poly)
    outFeature.SetField(fieldName1, fieldValue1)
    outFeature.SetField(fieldName2, fieldValue2)
    outLayer.CreateFeature(outFeature)

    ds.Destroy()


if __name__ == '__main__':
    createShpfile()
    # CreateShapeGeometry()